package com.freeseed.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.freeseed.model.entity.Role;
import com.freeseed.model.entity.User;
import com.freeseed.model.entity.UserDtl;
import com.freeseed.model.mapper.RoleMapper;
import com.freeseed.model.mapper.UserDtlMapper;
import com.freeseed.model.mapper.UserMapper;
import com.freeseed.model.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freeseed.util.BCryptPasswordEncoderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Allwayz
 * @since 2020-03-20
 */
@Slf4j
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserDtlMapper userDtlMapper;
    @Autowired
    private BCryptPasswordEncoderUtil bCryptPasswordEncoderUtil;

    /**
     * 验证登陆
     * @param username 账号
     * @param rawPassword 原始密码
     * @return
     * @throws Exception
     */
    @Override
    public boolean checkLogin(String username, String rawPassword) throws Exception {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("user_email",username));
        log.info(user.toString());
        if(user == null){
            throw new Exception("Account does not exist");
        }else {
            //加密的密码
            String encodedPassword = user.getUserPassword();
            log.info("input Password:    {}",rawPassword);
            log.info("Database Password: {}",encodedPassword);
            if(!bCryptPasswordEncoderUtil.matches(rawPassword,encodedPassword)) {
                throw new Exception("Password incorrect！");
            }else{
                log.info("Password is Correct");
                return true;
            }
        }
    }

    @Override
    public boolean register(String email, String password,String role) throws Exception {
        if(email != null && password != null && role != null){
            User f = userMapper.selectOne(new QueryWrapper<User>().eq("user_email",email));
            if(f != null){
                throw new Exception("User already exist");
            }
            int roleId = roleMapper.selectOne(new QueryWrapper<Role>().eq("role_desc",role)).getRoleId();
            String encodePassword = bCryptPasswordEncoderUtil.encode(password);
            User user = User.builder().userEmail(email).userPassword(encodePassword).roleId(roleId).build();
            userMapper.insert(user);
            UserDtl userDtl = UserDtl.builder().userId(userMapper.selectCount(new QueryWrapper<>())).build();
            userDtlMapper.insert(userDtl);
            return true;
        }else {
            throw new Exception("Error Message: java.lang.NullPointerException");
        }
    }
}
